const moment = require('moment')
const async = require('async')
const Member = require('../../model/Member')
const MemberCard = require('../../model/MemberCard')
const UserPoint = require('../../model/userPoint')
const PointHistory = require('../../model/PointHistory')
const User = require('../../model/User')
const Staff = require('../../model/Staff')
const wxPublicAccessToken = require('../../util/businessUtil/wxPublicAccessToken')
const request = require('request')

module.exports = {


    deteleAll: async(req, res, next) => {
        try {
            if (req.body.cellphone) {
                let user = await User.findOne({ cellphone: req.body.cellphone, isDelete: 0 }).exec()
                let openid = user.openid
                await User.findOneAndDelete({ cellphone: req.body.cellphone, isDelete: 0 }).exec()
                await Staff.findOneAndDelete({ cellphone: req.body.cellphone, isDelete: 0 }).exec()
                let member = await Member.findOne({ openid: openid, isDelete: 0 }).exec()
                member.code = ""
                await member.save()
            }
            return res.ok('删除成功')
        } catch (e) {
            next(e)
        }
    },

    userCode:async(req,res,next)=>{
        try{
            
            wxPublicAccessToken.getAccessToken(function(access_token) {
                var wxurl = "https://api.weixin.qq.com/card/user/getcardlist?access_token=" + access_token
                let postdata = {
                    "openid": "o472v4l9qz1GgenAdWDtwM-ycmpA",
                    "card_id": "pVBEys-PMXWcQDbflJyERqF-24Ko"
                }
                request({
                    url: wxurl,
                    method: "POST",
                    timeout: 10000,
                    body: postdata,
                    followRedirect: true,
                    json: true,
                    maxRedirects: 10
                }, function(error, response, body) {
                    console.log(error, body, 'dd')
                    res.ok('查询成功',body)
                })
            })
        }catch(e){

        }
    },

    newUserGivePoint:async(req,res,next)=>{
        try{
            let userList = await User.find({}).exec()
            for(let i=0;i<userList.length;i++){
                let member = await Member.findOne({openid:userList[i].openid}).exec()
                let pointHistory = await PointHistory.findOne({openid:userList[i].openid,amount:100,expenseHistory:null,type:0,expenseDate:{$gte:new Date("2020-08-24")}}).exec()
                if(member !=null){
                    if(pointHistory == null){
                        console.log('0')
                        member.newUserGivePoint = 0
                        await member.save()
                    }else{
                        console.log('100')
                        member.newUserGivePoint = 100
                        await member.save()
                    }
                }
            }
            // userList.forEach(async e=>{
            //     let member = await Member.findOne({openid:e.openid}).exec()
            //     let pointHistory = await PointHistory.findOne({openid:e.openid,amount:10000,expenseHistory:null,type:0,expenseDate:{$gte:new Date("2020-08-24")}}).exec()
            //     if(pointHistory == null){
            //         member.newUserGivePoint = 0
            //         await member.save()
            //     }else{
            //         member.newUserGivePoint = 100
            //         await member.save()
            //     }
            // })
        }catch(e){

        }
    },

    updateCode: async(req, res, next) => {
        try {
            let user = await User.findOne({cellphone:req.body.cellphone,isDelete:0}).exec()
            let member = await Member.findOne({openid:user.openid,isDelete:0}).exec()
            let memberCardNumber = (await MemberCard.findOne({ isDelete: 0 }).exec()).cardId
            let userPoint = await UserPoint.findOne({ openid: user.openid, isDelete: 0 }).exec()
            // if(member.code !== ""){
            //     return res.send({code:201,msg:"卡号已经存在"})
            // }
            member.code = req.body.code
            member.point = req.body.point
            member.updatedAt = new Date()
            await member.save()
            userPoint.current = req.body.point
            userPoint.updatedAt = new Date()
            await userPoint.save()
            wxPublicAccessToken.getAccessToken(function (token) {
                request({
                    url: 'https://api.weixin.qq.com/card/membercard/updateuser?access_token=' + token,
                    method: "POST",
                    timeout: 10000,
                    encoding: null,
                    json: {
                        "code": member.code,
                        "card_id": memberCardNumber,
                        "bonus": member.point
                    }
                }, function (error, response, body) {
                    console.log(error)
                    res.ok('添加会员卡号完成')
                })
            })
        } catch (e) {
            next(e)
        }
    },


};